package NC43_没有重复项数字的所有排列;

import java.util.*;

public class Solution {
    public ArrayList<ArrayList<Integer>> permute(int[] num) {
        ArrayList<ArrayList<Integer>> lists = new ArrayList<>();
        ArrayList<Integer> list = new ArrayList<>();
        backtrack(num, lists, list);

        return lists;
    }

    private void backtrack(int[] num, ArrayList<ArrayList<Integer>> lists, ArrayList<Integer> list) {
        if (list.size() == num.length){
            lists.add(new ArrayList<>(list));
            return;
        }
        for (int i = 0; i < num.length; i++){
            if (list.contains(num[i])){
                continue;
            }
            list.add(num[i]);
            backtrack(num, lists, list);
            list.remove(list.size() - 1);
        }
    }
}